﻿using System;
using System.Text.RegularExpressions;
using StartReading.Security.Validation;

namespace StartReading.Security.Validation
{
    public class EmailValidation : IValidation<String>
    {
        private const string EmailPattern = @"^([A-Za-z0-9._]+)@" +
                                           "(outlook|hotmail|live|msn|gmail|yahoo|aol)." +
                                           "(((com)((.[a-z]{2})|))|" +
                                           "(co.[a-z]{2}))$";

        private EmailValidation()
        {
            this.EmptyStringValidator = EmptyStringValidation.Instance;
            this.RegexValidator = RegexValidation.Instance;
        }

        #region Lazy Singleton Thread-Safe
        private static class EmailValidationHolder
        {
            static EmailValidationHolder()
            {
            }

            internal static readonly EmailValidation EmailValidation = new EmailValidation();
        }
        
        /// <summary>
        /// Gets an instance of EmailValidation class
        /// </summary>
        public static EmailValidation Instance
        {
            get
            {
                return EmailValidationHolder.EmailValidation;
            }
        }
        #endregion

        private IValidation<String, String> EmptyStringValidator { get; set; }
        private IValidation<String, String, String> RegexValidator { get; set; }

        /// <summary>
        ///     Validates an email. 
        ///     Only Outlook, Hotmail, MSN, Gmail, Live, Yahoo and AOL domains are accepted.
        ///     Throws ArgumentException if email is invalid.
        /// </summary>
        /// <param name="email">An Email to be validated</param>
        /// <exception cref="System.ArgumentException"></exception>
        public void IsValid( string email )
        {
            this.EmptyStringValidator.IsValid( email, "Email" );
            this.RegexValidator.IsValid( email, EmailValidation.EmailPattern,
                "Only Outlook, Hotmail, Live, MSN, GMail, Yahoo and AOL email accepted" );
        }        
    }
}
